import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import sun.security.util.Password;

// this page by fraser hogg
/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class PasswordDialogue extends javax.swing.JFrame {
	private JLabel lblUser;
	private JTextField txtUserId;
	private JLabel lblPass;
	private JButton btnSubmit;
	private JButton btnCancel;
	private JPasswordField txtPass;
	private int form;

	// taken verbatim from pkg
	private  Statement getCon() throws ClassNotFoundException, SQLException
	{
		Class.forName("com.mysql.jdbc.Driver");
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/travelexperts", "root","root");
		if (!conn.isClosed())
			System.out.println("Successfully connected.");
		return conn.createStatement();
	}
	//password validation
	private boolean valid(String agtId, String pass){
		boolean valid = false;
		ResultSet rs;
		Statement stmt;
		try {
			stmt = getCon();
			rs = stmt.executeQuery("SELECT AgtPwd FROM agents "+
					"WHERE AgentId='"+agtId+"' AND AgtPwd='"+pass+"'");
			if(rs.next()){
				valid = true;				
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return valid;
	}
	
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				PasswordDialogue inst = new PasswordDialogue();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public PasswordDialogue() {
		super();
		initGUI();
	}
	//overload for page handling
	public PasswordDialogue(int page) {
		super();
		initGUI();
		form = page;
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Travel Experts Login");
			getContentPane().setLayout(null);
			this.setResizable(false);
			{
				lblUser = new JLabel();
				getContentPane().add(lblUser);
				lblUser.setText("Agent ID:");
				lblUser.setBounds(22, 12, 57, 15);
				lblUser.setFont(new java.awt.Font("SansSerif",0,12));
			}
			{
				txtUserId = new JTextField();
				getContentPane().add(txtUserId);
				txtUserId.setBounds(81, 9, 146, 22);
			}
			{
				lblPass = new JLabel();
				getContentPane().add(lblPass);
				lblPass.setText("Password:");
				lblPass.setBounds(12, 46, 75, 15);
				lblPass.setFont(new java.awt.Font("SansSerif",0,12));
			}
			{
				txtPass = new JPasswordField();
				getContentPane().add(txtPass);
				txtPass.setBounds(82, 43, 145, 22);
			}
			{
				btnSubmit = new JButton();
				getContentPane().add(btnSubmit);
				btnSubmit.setText("Submit");
				btnSubmit.setBounds(34, 83, 78, 22);
				btnSubmit.setFont(new java.awt.Font("SansSerif",0,12));
				btnSubmit.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						String pass = "";
						char[] cs = txtPass.getPassword();
						for (int i=0; i<cs.length ;i++){
							pass += cs[i];
						}
						if(valid(txtUserId.getText(),pass)){
							switch (form){
							case 1:
								home inst = new home();
								inst.setLocationRelativeTo(null);
								inst.setVisible(true);
								PasswordDialogue.this.dispose();
								break;
							case 2:
								agent page2 = new agent();
								page2.setLocationRelativeTo(null);
								page2.setVisible(true);
								PasswordDialogue.this.dispose();
								break;
							case 3:
								pkg page3 = new pkg();
								page3.setLocationRelativeTo(null);
								page3.setVisible(true);
								PasswordDialogue.this.dispose();
								break;
							case 4:
								PkgProd page4 = new PkgProd();
								page4.setLocationRelativeTo(null);
								page4.setVisible(true);
								PasswordDialogue.this.dispose();
								break;
							}
						}
						else{
							PasswordDialogue.this.dispose();
							home inst = new home();
							inst.setLocationRelativeTo(null);
							inst.setVisible(true);
						}
					}
				});
			}
			{
				btnCancel = new JButton();
				getContentPane().add(btnCancel);
				btnCancel.setText("Cancel");
				btnCancel.setBounds(129, 83, 78, 22);
				btnCancel.setFont(new java.awt.Font("SansSerif",0,12));
				//Close the dialog when cancel clicked box Mike
				btnCancel.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						System.out.println("btnCancel.mouseClicked, event="+evt);
						PasswordDialogue.this.setVisible(false);
						home inst = new home();
						inst.setLocationRelativeTo(null);
						inst.setVisible(true);
					}
				});
			}
			pack();
			this.setSize(253, 156);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
